//
//  WWTabBar.swift
//  SinWEiBo
//
//  Created by 胡良志 on 2018/3/11.
//  Copyright © 2018年 HuLiangZhi. All rights reserved.
//

import UIKit

class WWTabBar: UITabBar {

    //MARK: 定义闭包:把view视图的点击事件传递给控制器
    var composeClick:(() -> ())?
    
    override init(frame: CGRect) {
        super .init(frame: frame)
        self.addSubview(KButton)
        KButton.addTarget(self, action: #selector(clickbuuton), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //按钮的点击事件
    @objc private func clickbuuton (){
        composeClick?()
    }

    
    //定义中间的按钮
    lazy var KButton: UIButton = {

        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
       // btn.frame = CGRect(x:UIScreen.main.bounds.width / 5, y: 0, width: 50, height: 50)
//        btn.sizeToFit()
        return btn
    }()
    
    //布局子控件会调用这个方法
    override func layoutSubviews() {
        super .layoutSubviews()
        //遍历子视图发现系统的UITabBarButton
//        print(subviews)
        let Width = UIScreen.main.bounds.width / 5
        let height = self.bounds.height
        //记录在View中的位置
        var index : CGFloat = 0
        //遍历子视图
        for suView in subviews {
            if suView .isKind(of: NSClassFromString("UITabBarButton")!){
                suView.frame = CGRect(x: index * Width, y: 0, width: Width, height: height)
                index += (index == 1 ? 2 : 1)
            }
        }
        //约束中间button的位置
        KButton.bounds.size = CGSize(width: Width, height: height)
        KButton.center = CGPoint(x: self.center.x, y: height * 0.5)
    }
    
    
    
    
    

}
